#ifndef __HYDROLOGY_H__
#define __HYDROLOGY_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "common.h"

#define FRAME_HEAD							0x7E
#define FRAME_STX							0x02
#define FRAME_SYN							0x16
#define FRAME_ETX							0x03
#define FRAME_ETB							0x17
#define FRAME_HEAD_LEN						2
#define FRAME_REMOTER_ADDR_LEN				5
#define FRAME_PASSWD_LEN					2
#define FRAME_PKGLEN_LEN					2
#define FRAME_PADDING_LEN					17

#define FRAME_OFFSET_CENTER_ADDR			FRAME_HEAD_LEN
#define FRAME_OFFSET_PASSWD					(FRAME_HEAD_LEN + 1 + FRAME_REMOTER_ADDR_LEN)
#define FRAME_OFFSET_CMDTYPE				(FRAME_HEAD_LEN + 1 + FRAME_REMOTER_ADDR_LEN + FRAME_PASSWD_LEN)
#define FRAME_OFFSET_PKGBEGIN				(FRAME_HEAD_LEN + 1 + FRAME_REMOTER_ADDR_LEN + FRAME_PASSWD_LEN + 1 + FRAME_PKGLEN_LEN)//2+1+5+2+1+2=13
#define FRAME_OFFSET_PKGDATA				(FRAME_HEAD_LEN + 1 + FRAME_REMOTER_ADDR_LEN + FRAME_PASSWD_LEN + 1 + FRAME_PKGLEN_LEN + 1)//2+1+5+2+1+2+1=14

#define DELAY_US_100MS		100000		//短指令延时 100ms
#define DELAY_US_300MS		300000		//语音指令延时	300ms
#define DELAY_US_1000MS		1000000		//图片指令延时	1000ms

#define	WATER_LEVEL_1	220
#define	WATER_LEVEL_2	250
#define	WATER_LEVEL_3	280
#define	ALARM_INTERVAL	10800

//#define	WATER_LEVEL_1	10
//#define	WATER_LEVEL_2	15
//#define	WATER_LEVEL_3	20
//#define	ALARM_INTERVAL	300

typedef struct {
	unsigned char center_addr;
	unsigned char remoter_addr[FRAME_REMOTER_ADDR_LEN];
} center_dst_t;

typedef struct {
	unsigned char remoter_addr[FRAME_REMOTER_ADDR_LEN];
	unsigned char center_addr;
} remoter_dst_t;

typedef struct {
	unsigned char header[FRAME_HEAD_LEN];		//2byte
	union {
		center_dst_t center_addrs;
		remoter_dst_t remoter_addrs;
	} dev_addr;									//6byte
	unsigned char passwd[FRAME_PASSWD_LEN];		//2
	unsigned char cmd_type;						//1
	unsigned char pkg_len[FRAME_PKGLEN_LEN];	//2
	unsigned char pkg_begin;					//1
	unsigned char pkg_data;						//1
	
	//unsigned char pkg_end;					//1
	//unsigned short CRC;						//2
} __attribute__((aligned(1))) hydrology_head_t;

typedef struct {
	unsigned char header[FRAME_HEAD_LEN];
	union {
		center_dst_t center_addrs;
		remoter_dst_t remoter_addrs;
	} dev_addr;
	unsigned char passwd[FRAME_PASSWD_LEN];
	unsigned char cmd_type;
	unsigned char pkg_len[FRAME_PKGLEN_LEN];
	unsigned char pkg_begin;
	unsigned char pkg_total[3];
	unsigned char pkg_data;
} __attribute__((aligned(8))) hydrology_syn_head_t;

typedef struct {
	unsigned int offset_start;	//数据包起始偏移		 	lseek(fd, 30, SEEK_SET)
	unsigned int offset_end;	//数据包结束偏移			write(fd, buf, offset_start-offset_end);
	unsigned int total_size;	//文件总大小
	char file_name[64]; 		//文件名
	char file_md5sum[32];		//文件MD5
	unsigned char data[0];		//数据,最大800字节	
} __attribute__((aligned(1))) hydrology_data_send_file_t;

typedef struct {
	char dev_sn[32];
	bool prohibit_send_mqtt;
	bool prohibit_send_serial;
	unsigned short addr;
	unsigned char cmdtype;
	void *buf;
	unsigned short length;
} hydrology_param_t;

typedef enum {
	HYD_FRAME_HEAD = 0,  
	HYD_FRAME_CENTER_ADDR, 
	HYD_FRAME_REMOTER_ADDR, 
	HYD_FRAME_PASSWORD, 
	HYD_FRAME_CMDTYPE, 
	HYD_FRAME_PKGLEN, 
	HYD_FRAME_BEGIN, 
	HYD_FRAME_DATA, 
	HYD_FRAME_END, 
	HYD_FRAME_CRC1, 
	HYD_FRAME_CRC2
} hydrology_stu_t;

#define HYD_CMD_ALIVE						0xE0	//报警器心跳
#define HYD_CMD_WARN						0xE1	//发送警报
#define HYD_CMD_RECORD						0xE2	//开始录音
#define HYD_CMD_RECORD_END					0xE3	//录音操作 确认 取消
#define HYD_CMD_PICTURE						0xE4	//图片更新
#define HYD_CMD_WARNCANCLE					0xE5	//消除警报

#define HYD_CMD_GET_ONLINE					0xE6	//开机广播获取节点在线状态
#define HYD_CMD_TRIGGER_ALARM				0xE6	//RTU 窄带触发报警报文

#define HYD_CMD_SET_NETINFO					0xE7	//设置报警器网络信息

#define HYD_CMD_PUSH_FILE					0xE8	//发送文件

#define HYD_CMD_BROADCAST_TASK				0xE9	//广播任务

#define DATA_TYPE_MQTT						0x01
#define DATA_TYPE_TPUNB						0x02

extern void hydrology_analysis(unsigned char *pkg, int data_type);
extern void hydrology_transform(unsigned char cmdtype, unsigned char *buf, unsigned short length);
extern void hydrology_transform_enqueue(hydrology_param_t *hydrology_param);
extern unsigned short do_crc16(unsigned char *ptr, int len);
extern void hydrology_waring_process(unsigned char *pkg);
extern void hydrology_voice_end(void);

extern void hydrology_voice_req(unsigned char *buf, unsigned short length);

extern void hydrology_transform_rtu_rsp(unsigned char *pkg_recv, unsigned char *buf, unsigned short length);

#ifdef __cplusplus
}
#endif

#endif

